package it.giuseppefrattura.wwdefence;


import android.graphics.Typeface;

import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import java.io.IOException;
import java.io.InputStream;




public class MainActivity extends SimpleBaseGameActivity {

	private static int CAMERA_WIDTH = 800;
	private static int CAMERA_HEIGHT = 480;
	
	ITextureRegion mBackgroundTextureRegion;
	public ITextureRegion mSol1, mSol2, mEn,mMatt;
	
	Camera mCamera;
	
    public Font mFont;

    //A reference to the current scene
    public Scene mCurrentScene;
    public static MainActivity instance;
    
	int enemynumber;
	
	@Override
	public EngineOptions onCreateEngineOptions() {
	    instance = this;

		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
		    new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
	}

	@Override
	protected void onCreateResources() {		
		
		mFont = FontFactory.create(this.getFontManager(),this.getTextureManager(), 256, 256,Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
		mFont.load();


		try {
		    // 1 - Set up bitmap textures
		    ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
		        @Override
		        public InputStream open() throws IOException {
		            return getAssets().open("gfx/mappa.png");
		        }
		    });
		    ITexture sol1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
		        @Override
		        public InputStream open() throws IOException {
		            return getAssets().open("gfx/sol1.png");
		        }
		    });
		    ITexture sol2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
		        @Override
		        public InputStream open() throws IOException {
		            return getAssets().open("gfx/sol2.png");
		        }
		    });
		    ITexture en = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
		        @Override
		        public InputStream open() throws IOException {
		            return getAssets().open("gfx/enemy.png");
		        }
		    });
		    
		    ITexture mat = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
		        @Override
		        public InputStream open() throws IOException {
		            return getAssets().open("gfx/mattonella1.jpg");
		        }
		    });
		    
		    // 2 - Load bitmap textures into VRAM
		    backgroundTexture.load();
		    sol1.load();
		    sol2.load();
		    en.load();

		    mat.load();
		    
		    // 3 - Set up texture regions
		    this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
		    this.mSol1  = TextureRegionFactory.extractFromTexture(sol1);
		    this.mSol2  = TextureRegionFactory.extractFromTexture(sol2);
		    this.mEn    = TextureRegionFactory.extractFromTexture(en);
		    this.mMatt  = TextureRegionFactory.extractFromTexture(mat);

		    
		} catch (IOException e) {
		    Debug.e(e);
		}
		
	}

	@Override
	public Scene onCreateScene() {
		//Inizio dallo splash Screen
		mCurrentScene = new SplashScene();
		return mCurrentScene;	
	}
	
	public static MainActivity getSharedInstance() {
	    return instance;
	}


	// to change the current main scene
	public void setCurrentScene(Scene scene) {
	    mCurrentScene = scene;
	    getEngine().setScene(mCurrentScene);
	}



}

